
clear all
set more off

** Change path to re-run on another computer
cd ""
**


use "Energy_prices", clear

*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*0) gen OECD regional price index and OECD id.
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
foreach x in autodiesel_nomindex autodiesel_realindex coal_nomindex coal_realindex electricity_nomindex electricity_realindex heavyfueloil_nomindex heavyfueloil_realindex lightfueloil_nomindex lightfueloil_realindex naturalgas_nomindex naturalgas_realindex oilproducts_nomindex oilproducts_realindex totalenergy_nomindex totalenergy_realindex{
g `x'_OECD_r=`x'_OECD_E
replace `x'_OECD_r=`x'_OECD_A if cou=="Brazil" | cou=="Canada" | cou=="Chile" | cou=="Mexico" | cou=="United States of America" | cou=="Venezuela (Bolivarian Republic of)" 
replace `x'_OECD_r=`x'_OECD_AO if cou=="Australia" | cou=="China" | cou=="India" | cou=="Indonesia" | cou=="Japan" | cou=="Kazakhstan" | cou=="Korea, Republic of" | cou=="New Zealand" | cou=="Pakistan" | cou=="Singapore" | cou=="Taiwan, Republic of China" | cou=="Thailand"
replace `x'_OECD_r=`x'_OECD_AO if cou=="Ghana" | cou=="South Africa" 
}

gen OECD=1 if isoalpha3 == "AUS" | isoalpha3 == "AUT" | isoalpha3 == "BEL" | isoalpha3 == "CAN" | isoalpha3 == "CHE" | isoalpha3 == "CHL" | isoalpha3 == "CZE" | isoalpha3 == "DEU" | isoalpha3 == "DNK" | isoalpha3 == "ESP" | isoalpha3 == "EST" | isoalpha3 == "FIN" | isoalpha3 == "FRA" | isoalpha3 == "GBR" | isoalpha3 == "GRC" | isoalpha3 == "HUN" | isoalpha3 == "IRL" | isoalpha3 == "ISR" | isoalpha3 == "ITA" | isoalpha3 == "JPN" | isoalpha3 == "KOR" | isoalpha3 == "LUX" | isoalpha3 == "MEX" | isoalpha3 == "NLD" | isoalpha3 == "NOR" | isoalpha3 == "NZL" | isoalpha3 == "POL" | isoalpha3 == "PRT" | isoalpha3 == "SVK" | isoalpha3 == "SVN" | isoalpha3 == "SWE" | isoalpha3 == "TUR" | isoalpha3 == "USA"
replace OECD= 0 if OECD==.


xtset isonum year, yearly


*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
* 1) Use growth rate from RESPECTIVE (i.e. gas, oil..) real price index for OECD countries 
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$



local rex "rex_oil_heavy rex_electricity rex_gas rex_coal" // rex_oil_tot

foreach x of local rex{
	gen growth_`x'=(`x'-L.`x')/L.`x'
}

rename growth_rex_oil_heavy  growth_rex_oil

//only take what will use:
local fuel "oil_hs_consUS gas_consUS coal_steam_consUS electricity_consUS oil_hs_PPPcons gas_PPPcons coal_steam_PPPcons electricity_PPPcons"

foreach x of local fuel{
	*gen `x'_respindex=`x'
	*label var `x'_respindex "Price in const USD (missing imputed with respective real price index)"
	g `x'_respindex_L=`x'
	g `x'_respindex_F=`x'
}

rename oil_hs_consUS_respindex* oil_consUS_respindex*
rename coal_steam_consUS_respindex* coal_consUS_respindex*

rename oil_hs_PPPcons_respindex* oil_PPPcons_respindex*
rename coal_steam_PPPcons_respindex* coal_PPPcons_respindex*

local fuel "oil gas coal electricity"

foreach x of local fuel{
	forvalues i = 1/60 {
		replace `x'_consUS_respindex_L=L.`x'_consUS_respindex_L*(1+growth_rex_`x') if `x'_consUS_respindex_L ==.
		replace `x'_PPPcons_respindex_L=L.`x'_PPPcons_respindex_L*(1+growth_rex_`x') if `x'_PPPcons_respindex_L ==.
	}
	forvalues i = 1/60 {
		replace `x'_consUS_respindex_F=F.`x'_consUS_respindex_F/(1+F.growth_rex_`x') if `x'_consUS_respindex_F ==.
		replace `x'_PPPcons_respindex_F=F.`x'_PPPcons_respindex_F/(1+F.growth_rex_`x') if `x'_PPPcons_respindex_F ==.
	}
	*now take mean
	egen `x'_consUS_respindex=rowmean(`x'_consUS_respindex_L `x'_consUS_respindex_F)
	label var `x'_consUS_respindex "Price in cons USD (missing imputed with respective real price index)"
	egen `x'_PPPcons_respindex=rowmean(`x'_PPPcons_respindex_L `x'_PPPcons_respindex_F)
	label var `x'_PPPcons_respindex "Price in cons USD (missing imputed with respective real price index)"
}

*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*** 2) Use growth rate from real RESPECTIVE WHOLESALE price index (i.e. gas, oil..) for NON-OECD countries MAINLY, but some for coal, and one or two obs for gas.
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

local wi "wi_oilproducts wi_electricity wi_naturalgas wi_coal"

foreach x of local wi{
	gen growth_`x'=(`x'-L.`x')/L.`x'
}

rename (growth_wi_oilproducts growth_wi_naturalgas)  (growth_wi_oil  growth_wi_gas)


*check that no wi where we have rex
list cou year if rex_oil_heavy==. & wi_oil!=. & OECD==1 & oil_hs_consUS==. & year>1995
list cou year if rex_coal==. & wi_coal!=. & OECD==1 & coal_steam_consUS==. & year>1995
// quite a few, the rest there are none
list cou year if rex_gas==. & wi_naturalgas!=. & OECD==1 & gas_consUS==. & year>1995
list cou year if rex_electricity==. & wi_elec!=. & OECD==1 & electricity_consUS==. & year>1995

sum *_consUS_respindex if OECD==1
/*
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
oil_consUS~x |      1,064    441.6324    244.9697          0    1634.74
gas_c~pindex |        985    436.0062     236.063   52.14067   2243.482
coal_consU~x |        720    203.2079     117.052          0   1108.998
electricit.. |      1,175    1318.699    515.5642   463.0721   3619.523
*/

local fuel "oil gas coal electricity"

foreach x of local fuel{
	forvalues i = 1/60 {
		replace `x'_consUS_respindex_L=L.`x'_consUS_respindex_L*(1+growth_wi_`x') if `x'_consUS_respindex_L ==. // & OECD==0
		replace `x'_PPPcons_respindex_L=L.`x'_PPPcons_respindex_L*(1+growth_wi_`x') if `x'_PPPcons_respindex_L ==. // & OECD==0
	}
	forvalues i = 1/60 {
		replace `x'_consUS_respindex_F=F.`x'_consUS_respindex_F/(1+F.growth_wi_`x') if `x'_consUS_respindex_F ==. // & OECD==0
		replace `x'_PPPcons_respindex_F=F.`x'_PPPcons_respindex_F/(1+F.growth_wi_`x') if `x'_PPPcons_respindex_F ==. // & OECD==0
	}
	*now take mean
	egen `x'_consUS_respindext=rowmean(`x'_consUS_respindex_L `x'_consUS_respindex_F)
	egen `x'_PPPcons_respindext=rowmean(`x'_PPPcons_respindex_L `x'_PPPcons_respindex_F)
	
	replace `x'_consUS_respindex=`x'_consUS_respindext if  `x'_consUS_respindex==. // OECD==0 &
	replace `x'_PPPcons_respindex=`x'_PPPcons_respindext if  `x'_PPPcons_respindex==. // OECD==0 &

}

drop *respindex_F *respindex_L *respindext

sum *_consUS_respindex if OECD==1
/*
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
oil_consUS~x |      1,064    441.6324    244.9697          0    1634.74
gas_c~pindex |        987    436.4684    236.2437   52.14067   2243.482
coal_consU~x |        766    198.8291    114.9216          0   1108.998
electricit.. |      1,175    1318.699    515.5642   463.0721   3619.523
*/
 

*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*** 3) Create resptot by applying the growht rate from total index to the already imputed respind
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

gen growth_rex=(rex_totenergy-L.rex_totenergy)/L.rex_totenergy

local fuel "oil gas coal electricity"

foreach x of local fuel{
	g `x'_consUS_resptot_L=`x'_consUS_respindex
	g `x'_consUS_resptot_F=`x'_consUS_respindex
	g `x'_PPPcons_resptot_L=`x'_PPPcons_respindex
	g `x'_PPPcons_resptot_F=`x'_PPPcons_respindex
	}

foreach x of local fuel{
	forvalues i = 1/60 {
		replace `x'_consUS_resptot_L =L.`x'_consUS_resptot_L*(1+growth_rex) if `x'_consUS_resptot_L ==.
		replace `x'_PPPcons_resptot_L =L.`x'_PPPcons_resptot_L*(1+growth_rex) if `x'_PPPcons_resptot_L ==.
	}
	forvalues i = 1/60 {
		replace `x'_consUS_resptot_F =F.`x'_consUS_resptot_F/(1+F.growth_rex) if `x'_consUS_resptot_F==.
		replace `x'_PPPcons_resptot_F =F.`x'_PPPcons_resptot_F/(1+F.growth_rex) if `x'_PPPcons_resptot_F==.
	}
	egen `x'_consUS_resptotindex=rowmean(`x'_consUS_resptot_L `x'_consUS_resptot_F )
	label var `x'_consUS_resptotindex "Price in cons USD (missing imputed with respective and total real price index)"
	egen `x'_PPPcons_resptotindex=rowmean(`x'_PPPcons_resptot_L `x'_PPPcons_resptot_F )
	label var `x'_PPPcons_resptotindex "Price in cons USD (missing imputed with respective and total real price index)"
}


*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*** 4) China: use datapoint for gas in 1997 and extent with an average of wholesale oil, electricity and coal until 2004 and then use the gas wholesale index
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

gen avg_growth_temp = (growth_wi_oil + growth_wi_electricity + growth_wi_coal)/3 if country=="China"
g gas_consUS_respindex_before=gas_consUS_respindex
g gas_PPPcons_respindex_before=gas_PPPcons_respindex

forvalues i = 1/20 {
		replace gas_consUS_respindex =F.gas_consUS_respindex/(1+F.avg_growth_temp) if gas_consUS_respindex ==. & country=="China" 
		replace gas_consUS_resptotindex =F.gas_consUS_resptotindex/(1+F.avg_growth_temp) if gas_consUS_resptotindex ==. & country=="China" 

	}

forvalues i = 1/7 {
	replace gas_consUS_respindex=L.gas_consUS_respindex*(1+avg_growth_temp) if gas_consUS_respindex ==. & country=="China" & year<2005
	replace gas_consUS_resptotindex =L.gas_consUS_resptotindex*(1+avg_growth_temp) if gas_consUS_resptotindex ==. & country=="China" & year<2005

}

forvalues i = 1/20 {
	replace gas_consUS_respindex=L.gas_consUS_respindex*(1+growth_wi_gas) if gas_consUS_respindex ==. & country=="China" & year>=2005
	replace gas_consUS_resptotindex =L.gas_consUS_resptotindex*(1+growth_wi_gas) if gas_consUS_resptotindex ==. & country=="China" & year>=2005
}

forvalues i = 1/20 {
		replace gas_PPPcons_respindex =F.gas_PPPcons_respindex/(1+F.avg_growth_temp) if gas_PPPcons_respindex ==. & country=="China" 
		replace gas_PPPcons_resptotindex =F.gas_PPPcons_resptotindex/(1+F.avg_growth_temp) if gas_PPPcons_resptotindex ==. & country=="China" 

	}

forvalues i = 1/7 {
	replace gas_PPPcons_respindex=L.gas_PPPcons_respindex*(1+avg_growth_temp) if gas_PPPcons_respindex ==. & country=="China" & year<2005
	replace gas_PPPcons_resptotindex =L.gas_PPPcons_resptotindex*(1+avg_growth_temp) if gas_PPPcons_resptotindex ==. & country=="China" & year<2005

}

forvalues i = 1/20 {
	replace gas_PPPcons_respindex=L.gas_PPPcons_respindex*(1+growth_wi_gas) if gas_PPPcons_respindex ==. & country=="China" & year>=2005
	replace gas_PPPcons_resptotindex =L.gas_PPPcons_resptotindex*(1+growth_wi_gas) if gas_PPPcons_resptotindex ==. & country=="China" & year>=2005
}
drop avg_growth_temp

rename gas_consUS_respindex gas_consUS_respindex_wCh
rename gas_consUS_respindex_before gas_consUS_respindex
rename gas_PPPcons_respindex gas_PPPcons_respindex_wCh
rename gas_PPPcons_respindex_before gas_PPPcons_respindex

rename *resptotindex *restotOLD
drop *resptot_F *resptot_L

*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*** 5) Create alternative imputation by applying the growht rate from OECD regional index 
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
rename (heavyfueloil_realindex_OECD_r electricity_realindex_OECD_r coal_realindex_OECD_r naturalgas_realindex_OECD_r ) (oil_realindex_OECD_r electricity_realindex_OECD_r coal_realindex_OECD_r gas_realindex_OECD_r )

local fuel "oil gas coal electricity"

foreach x of local fuel{
	gen growth_OECDr_`x'=(`x'_realindex_OECD_r-L.`x'_realindex_OECD_r)/L.`x'_realindex_OECD_r
}

local fuel "oil gas coal electricity" 

foreach x of local fuel{
	g `x'_consUS_resptot_L=`x'_consUS_respindex
	g `x'_consUS_resptot_F=`x'_consUS_respindex
	g `x'_PPPcons_resptot_L=`x'_PPPcons_respindex
	g `x'_PPPcons_resptot_F=`x'_PPPcons_respindex
	}


foreach x of local fuel{
	forvalues i = 1/60 {
		replace `x'_consUS_resptot_L =L.`x'_consUS_resptot_L*(1+growth_OECDr_`x') if `x'_consUS_resptot_L ==.
		replace `x'_PPPcons_resptot_L =L.`x'_PPPcons_resptot_L*(1+growth_OECDr_`x') if `x'_PPPcons_resptot_L ==.
	}
	forvalues i = 1/60 {
		replace `x'_consUS_resptot_F =F.`x'_consUS_resptot_F/(1+F.growth_OECDr_`x') if `x'_consUS_resptot_F==.
		replace `x'_PPPcons_resptot_F =F.`x'_PPPcons_resptot_F/(1+F.growth_OECDr_`x') if `x'_PPPcons_resptot_F==.
	}
	egen `x'_consUS_resptotindex=rowmean(`x'_consUS_resptot_L `x'_consUS_resptot_F )
	label var `x'_consUS_resptotindex "Price in cons USD (missing imputed with respective and total real price index)"
	egen `x'_PPPcons_resptotindex=rowmean(`x'_PPPcons_resptot_L `x'_PPPcons_resptot_F )
	label var `x'_PPPcons_resptotindex "Price in cons USD (missing imputed with respective and total real price index)"
}


drop *resptot_F *resptot_L



*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
** 6) Use out of sample leave one out cross validation to see which imputation performs better by country
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


preserve
keep if year>1994

foreach fuel in oil gas coal electricity{
local fuel2 `fuel'
if "`fuel'" == "oil"{
local fuel2 "oil_hs"
}
if "`fuel'" == "coal"{
local fuel2 "coal_steam"
}

local x `fuel'

*** DECIDE ON BASE ***
*replace `x'_consUS_respindex=`fuel2'_consUS
***


* LEAVE ONE OUT loop:
*********************
g t1_`fuel'=.
g t2_`fuel'=.
g t1b_`fuel'=.
g t2b_`fuel'=.
g loo=_n if `x'_consUS_respindex!=.
levelsof loo, local(loo_n2)
foreach loo_n of local loo_n2{
g tempv=`x'_consUS_respindex
replace `x'_consUS_respindex=. if loo==`loo_n'

********************* RECALCULATE BOTH INDICES
// as basis take the respindex imputed

*tot energy
	g `x'_consUS_resptot_L=`x'_consUS_respindex
	g `x'_consUS_resptot_F=`x'_consUS_respindex
	g `x'_PPPcons_resptot_L=`x'_PPPcons_respindex
	g `x'_PPPcons_resptot_F=`x'_PPPcons_respindex

	forvalues i = 1/60 {
		replace `x'_consUS_resptot_L =L.`x'_consUS_resptot_L*(1+growth_rex) if `x'_consUS_resptot_L ==.
		replace `x'_PPPcons_resptot_L =L.`x'_PPPcons_resptot_L*(1+growth_rex) if `x'_PPPcons_resptot_L ==.
	}
	forvalues i = 1/60 {
		replace `x'_consUS_resptot_F =F.`x'_consUS_resptot_F/(1+F.growth_rex) if `x'_consUS_resptot_F==.
		replace `x'_PPPcons_resptot_F =F.`x'_PPPcons_resptot_F/(1+F.growth_rex) if `x'_PPPcons_resptot_F==.
	}
	egen `x'_consUS_CV=rowmean(`x'_consUS_resptot_L `x'_consUS_resptot_F )
	egen `x'_PPPcons_CV=rowmean(`x'_PPPcons_resptot_L `x'_PPPcons_resptot_F )
drop *resptot_F *resptot_L

*OECD
	g `x'_consUS_resptot_L=`x'_consUS_respindex
	g `x'_consUS_resptot_F=`x'_consUS_respindex
	g `x'_PPPcons_resptot_L=`x'_PPPcons_respindex
	g `x'_PPPcons_resptot_F=`x'_PPPcons_respindex

	forvalues i = 1/60 {
		replace `x'_consUS_resptot_L =L.`x'_consUS_resptot_L*(1+growth_OECDr_`x') if `x'_consUS_resptot_L ==.
		replace `x'_PPPcons_resptot_L =L.`x'_PPPcons_resptot_L*(1+growth_OECDr_`x') if `x'_PPPcons_resptot_L ==.
	}
	forvalues i = 1/60 {
		replace `x'_consUS_resptot_F =F.`x'_consUS_resptot_F/(1+F.growth_OECDr_`x') if `x'_consUS_resptot_F==.
		replace `x'_PPPcons_resptot_F =F.`x'_PPPcons_resptot_F/(1+F.growth_OECDr_`x') if `x'_PPPcons_resptot_F==.
	}
	egen `x'_consUS_CVO=rowmean(`x'_consUS_resptot_L `x'_consUS_resptot_F )
	egen `x'_PPPcons_CVO=rowmean(`x'_PPPcons_resptot_L `x'_PPPcons_resptot_F )

drop *resptot_F *resptot_L

***********Calculate RMSE
// same sample, i.e non-OECD mostly where do additional predictions.
replace  t1_`fuel'=(`fuel'_consUS_CV-tempv)^2 if loo==`loo_n' & `fuel'_consUS_CV!=. & `fuel'_consUS_CVO!=. 
*replace  t1b_`fuel'=(`fuel'_consUS_CV-tempv)^2 if loo==`loo_n' & `fuel'_consUS_CV!=.   // is the exact same since oecd index available always so imputed also if the other imputed sinc same base
replace  t2_`fuel'=(`fuel'_consUS_CVO-tempv)^2 if loo==`loo_n' & `fuel'_consUS_CV!=. & `fuel'_consUS_CVO!=.
replace  t2b_`fuel'=(`fuel'_consUS_CVO-tempv)^2 if loo==`loo_n' & `fuel'_consUS_CVO!=. 

drop `x'_consUS_respindex *_CV *_CVO
rename tempv `x'_consUS_respindex
}
drop loo

egen mse_tote_`fuel'=mean(t1_`fuel') 
*egen mse_toteb_`fuel'=mean(t1b_`fuel') 
egen mse_oecd_`fuel'=mean(t2_`fuel') // same sample as toten, so no non-oecd
egen mse_oecdb_`fuel'=mean(t2b_`fuel') if OECD==1 // additional oecd that not in toten, *only chile and israel*
egen mse_oecd2_`fuel'=mean(t2b_`fuel') if OECD==0 // the nonoecd only
bysort cou: egen mse_tote_c_`fuel'=mean(t1_`fuel') 
bysort cou: egen mse_oecd_c_`fuel'=mean(t2_`fuel') 
assert t1_`fuel'==. if  t2_`fuel'==.
assert t2_`fuel'==. if  t1_`fuel'==.
bysort cou: egen mse_count_`fuel'=count(t1_`fuel')  // on how many obs is it based the rmse
sort isonum year
}


foreach x of varlist mse_*{
replace `x'=sqrt(`x')
}
foreach x of varlist mse_count*{
replace `x'=`x'^2
}

foreach fuel in oil gas coal electricity{
di "`fuel'"
sum mse_tote_`fuel' mse_oecd*_`fuel'
}

keep cou mse*
rename mse* rmse*
duplicates drop
save  "Energy_prices_imputed_RMSE_resp", replace
restore


*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
** 7) Use imputation that performs better by country
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

preserve
use  "Energy_prices_imputed_RMSE_resp", clear

drop *oecdb* *oecd2*
foreach x of varlist rmse*{
*replace `x'=round(`x',0.1)
}

foreach fuel in oil gas coal electricity{
g usetote_`fuel'=.
replace usetote_`fuel'=1 if rmse_tote_c_`fuel'<rmse_oecd_c_`fuel' & rmse_tote_c_`fuel'!=. & rmse_oecd_c_`fuel'!=.
replace usetote_`fuel'=0 if rmse_tote_c_`fuel'>rmse_oecd_c_`fuel' & rmse_tote_c_`fuel'!=. & rmse_oecd_c_`fuel'!=.
}
keep cou usetote_*
tempfile indchoice
save `indchoice'

restore


* Merge totindex choice back in 
***************************
merge m:1 country using `indchoice'
drop _m

*1) for countries where choose totindex:
****************

**first check how many post 1994
preserve
keep if year>1994
sort cou year
foreach x in oil gas coal electricity{
foreach y in consUS PPPcons{
di "`x'_`y'"
list cou year `x'_`y'_respindex  `x'_`y'_restotOLD `x'_`y'_resptotindex if  `x'_`y'_resptotindex!=`x'_`y'_restotOLD   & usetote_`x'==1 
replace `x'_`y'_resptotindex=`x'_`y'_restotOLD  if usetote_`x'==1
*check if for nonoecd
assert `x'_`y'_restotOLD==. if `x'_`y'_respindex==. & OECD==0 & cou!="China"
}
}
restore


foreach x in oil gas coal electricity{
foreach y in consUS PPPcons{
di "`x'_`y'"
list cou year `x'_`y'_respindex  `x'_`y'_restotOLD `x'_`y'_resptotindex if  `x'_`y'_resptotindex!=`x'_`y'_restotOLD   & usetote_`x'==1 
replace `x'_`y'_resptotindex=`x'_`y'_restotOLD  if usetote_`x'==1
*check if for nonoecd
assert `x'_`y'_restotOLD==. if `x'_`y'_respindex==. & OECD==0 & cou!="China"
}
}


*2) for non-OECD non EU:
****************
g OECD2=OECD
replace OECD2=1 if cou=="Bulgaria" | cou=="Croatia" | cou=="Cyprus" | cou=="Latvia" | cou=="Lithuania" | cou=="Malta" | cou=="Romania" 


***for non OECD replace resptot with resptot old, so all to missing. 

foreach x in oil gas coal electricity{
foreach y in consUS PPPcons{
replace `x'_`y'_resptotindex=`x'_`y'_restotOLD if OECD2==0
}
}
* for gas also china replaced:
sum gas_consUS_respindex_wCh gas_PPPcons_respindex_wCh gas_consUS_respindex gas_PPPcons_respindex

replace gas_consUS_respindex=gas_consUS_respindex_wCh if cou=="China"
replace gas_PPPcons_respindex=gas_PPPcons_respindex_wCh if cou=="China"

drop OECD2


*only keep the resptot index but create flag based on cou year
g flag_VEPL=.

g temp=0
g temp2=0
g temp3=0

rename (oil_hs_consUS coal_steam_consUS) (oil_consUS coal_consUS)

foreach fuel in oil gas coal electricity {
replace temp=temp+`fuel'_consUS_resptotindex
replace temp2=temp2+`fuel'_consUS_respindex
replace temp3=temp3+`fuel'_consUS
}
sum temp*

replace flag_VEPL=-1 if temp==. // all missing
replace flag_VEPL=2 if temp!=. // the resptot
replace flag_VEPL=1 if temp2!=. // all resp
replace flag_VEPL=0 if temp3!=. // obs
tab flag_VEPL
drop temp*

rename (oil_consUS coal_consUS) (oil_hs_consUS coal_steam_consUS) 


label var flag_VEPL "-1=missing, 0=all obs., 1=resp. index imp., 2=agg index imp."

save "Energy_prices_imputed(index)", replace

